home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 2 code / Using C++ objects / PtrObject.cp next >
Encoding:
Text File  |  1990-08-23  |  2.9 KB  |  124 lines  |  [TEXT/MPS ]

  1. // PtrObject.cp
  2. #include <Memory.h>
  3. #include <Errors.h>
  4. #include <stdio.h>
  5. #include <stddef.h>
  6.  
  7. #include "PtrObject.h"
  8.  
  9. // Static data members actually need to be declared outside of the class definition in
  10. // order to have space allocated.
  11. THz PtrObject::fZone = nil;
  12.  
  13.  
  14. OSErr PtrObject::AllocHeap(size_t heapSize)
  15. {
  16.     // By default, the heap gets kNumDfltMasters master pointers. A small number, but it 
  17.     // shouldn’t matter, since we will only be allocating Ptrs in this heap, and Ptrs
  18.     // don’t use master pointers. 
  19.     const short kNumDfltMasters = 16;
  20.  
  21.     // This magic number from Inside Mac, vol. II, chapter 1, is the amount of space
  22.     // required for the zone header and trailer, and the master pointer block. We add this
  23.     // to the requested heap size to compensate.
  24.     const size_t kZoneOverhead = 64 + 8 + (sizeof(long) * kNumDfltMasters);
  25.  
  26.     heapSize += kZoneOverhead;        // Factor in overhead.
  27.  
  28.     // Allocate space for the zone.
  29.     Ptr zonePtr = NewPtr(heapSize);    
  30.     if (!zonePtr)                    // if alloc fails, return error
  31.       return MemError();            // Get a pointer to the end of the heap.
  32.  
  33.     Ptr limitPtr = (Ptr) (((ptrdiff_t) zonePtr) + heapSize);
  34.  
  35.     // Initialize the zone.
  36.     THz  savedZone = GetZone();
  37.     InitZone(nil, kNumDfltMasters, limitPtr, zonePtr);
  38.     SetZone(savedZone);
  39.  
  40.     // Save the zone pointer in our static class variable.
  41.     fZone = (THz) zonePtr;
  42.     return noErr;
  43. }
  44.  
  45. void PtrObject::DisposeHeap()
  46. {
  47.     // If zone actually exists, dispose of it.
  48.     if (fZone)
  49.       {
  50.         DisposPtr((Ptr) fZone);
  51.         fZone = nil;
  52.       }
  53. }
  54.  
  55. long PtrObject::FreeMemory()
  56. {
  57.     THz savedZone;
  58.  
  59.     // Before we can return the amount of free memory, we
  60.     // need to switch to the correct zone.
  61.     if (fZone)
  62.       {
  63.         savedZone = GetZone();        // Save current zone.
  64.         SetZone(fZone);                // Make our zone current.
  65.       }
  66.  
  67.     long free = FreeMem();            // Get total free space.
  68.  
  69.     if (fZone)
  70.       SetZone(savedZone);            // Restore previous zone.
  71.  
  72.     return free;
  73. }
  74.  
  75. Size PtrObject::MaxMemory()
  76. {
  77.     THz savedZone;
  78.     
  79.     // Before we can return the maximum block size, we need
  80.     // to switch to the correct zone.
  81.     if (fZone)
  82.       {
  83.         savedZone = GetZone();        // Save current zone.
  84.         SetZone(fZone);                // Make our zone current.
  85.       }
  86.  
  87.     // We know the heap can’t grow, but we have to have a temp
  88.     // variable to satisfy the Toolbox.
  89.     Size tSize;
  90.  
  91.     Size max = MaxMem(&tSize);        // Get size of biggest block.
  92.  
  93.     if (fZone)
  94.       SetZone(savedZone);            // Restore previous zone.
  95.  
  96.     return max;
  97. }
  98.  
  99. void* PtrObject::operator new(size_t size)
  100. {
  101.     THz savedZone;
  102.  
  103.     // before we can allocate memory, we need
  104.     // to switch to the correct zone
  105.     if (fZone)
  106.       {
  107.         savedZone = GetZone();    // Save current zone.
  108.         SetZone(fZone);            // Make our zone current.
  109.       }
  110.  
  111.     Ptr p = NewPtr(size);        // Allocate memory for object.
  112.  
  113.     if (fZone)
  114.     SetZone(savedZone);            // Restore previous zone.
  115.  
  116.     return p;
  117. }
  118.  
  119. void PtrObject::operator delete(void* p)
  120. {
  121.     DisposPtr((Ptr) p);            // This works regardless of the zone
  122.                                 // the pointer was allocated in.
  123. }
  124.